<%@ page import="java.sql.ResultSet"%>
<%@ page import="java.sql.PreparedStatement"%>
<%@ page import="java.sql.DriverManager"%>
<%@ page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>社团活动</title>
    <link rel="stylesheet" href="../css/clubLife.css"> <!-- 引入CSS样式 -->
</head>
<body>

    <!-- 菜单栏 -->
    <header>
        <div class="header-container">
            <h1 class="logo">学校论坛</h1>
            <nav class="navbar">
                <ul>
                    <li><a href="../index.jsp">首页</a></li>
                    <li><a href="studyExchange.jsp">学习交流</a></li>
                    <li><a href="campusLife.jsp">校园生活</a></li>
                    <li><a href="clubLife.jsp">社团活动</a></li>
                    <li><a href="post.jsp">发表帖子</a></li>
                    <li><a href="myPosts.jsp">我的消息</a></li>
                </ul>
            </nav>
        </div>
    </header>

    <!-- 主要内容 -->
    <main>
        <h1>社团活动帖子</h1>

        <%
            String DB_URL = "jdbc:mysql://localhost:3306/lczmysql?useUnicode=true&characterEncoding=UTF-8";
            String DB_USER = "root";
            String DB_PASSWORD = "123456";
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
            String sql = "SELECT * FROM post WHERE category = '社团活动' ORDER BY post_time DESC"; // 查询社团活动
            PreparedStatement statement = connection.prepareStatement(sql);
            ResultSet resultSet = statement.executeQuery();
        %>

        <% while (resultSet.next()) { %>
        <article class="post">
            <div class="category"><%= resultSet.getString("category") %></div>
            <h2><%= resultSet.getString("title") %></h2>
            <p class="author-info">发布者: <%= resultSet.getString("username") %>  发布时间: <%= resultSet.getString("post_time") %> </p>
            
            <!-- 获取 content 内容并替换路径 -->
            <p class="excerpt">
                <%
                    String content = resultSet.getString("content");
                    if (content != null) {
                        // 替换 src="/ueditor 为 src="/luntan/ueditor 确保图片路径正确
                        content = content.replaceAll("src=\"/ueditor", "src=\"/luntan/ueditor");
                    }
                %>
                <%= content %>
            </p>

        </article>
        <% } %>

        <%
            resultSet.close();
            statement.close();
            connection.close();
        %>
    </main>

    <!-- 底部 -->
    <footer>
        <p>&copy; 2024 学校论坛版权所有</p>
    </footer>

</body>
</html>
